package com.wk.generator;



import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;

import java.util.ArrayList;
import java.util.List;

public class MyGenerator {

    public static void main(String[] args) {

        //代码生成器
        AutoGenerator mpg = new AutoGenerator();

        //全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("Wangkun");
        gc.setOpen(false);  //是否打开输出目录
        mpg.setGlobalConfig(gc);

        //数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/testjpa?characterEncoding=utf8&useSSL=false&serverTimezone=UTC");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        mpg.setDataSource(dsc);

        //包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("");
        pc.setParent("com.wk");
        pc.setController("controller");
        mpg.setPackageInfo(pc);

        //自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                //to do anything
            }
        };

        //如果模板引擎是freemarker
        //String templatePath = "templates/mapper.xml.ftl";
        // 如果模板引擎是 velocity
        String templatePath = "/templates/generators/mapper.xml.vm";

        //自定义输出配置
        List<FileOutConfig>  focList = new ArrayList<>();
        //自定义配置会被优先输出
        focList.add(new FileOutConfig(templatePath) {
            @Override
            public String outputFile(TableInfo tableInfo) {
                //自定义输出文件名，如果你的Entity设置了前缀，此处注意xml的名称会跟着发生变化！！
                return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
                        + "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
            }
        });

        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        //配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 配置自定义输出模板
        //指定自定义模板路径，注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
        // templateConfig.setEntity("templates/entity2.java");
        // templateConfig.setService();
        // templateConfig.setController();

        templateConfig.setXml(null);
        mpg.setTemplate(templateConfig);

        //策略配置
        StrategyConfig stragegy = new StrategyConfig();
        stragegy.setNaming(NamingStrategy.underline_to_camel);
        stragegy.setColumnNaming(NamingStrategy.underline_to_camel);

        //stragegy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        stragegy.setEntityLombokModel(true);

        stragegy.setRestControllerStyle(true);
        //stragegy.setSuperControllerClass("com.baomidou.ant.common.BaseController");

        stragegy.setInclude(new String[]{"t_manager","t_manager_role","t_permission","t_project_user","t_role","t_role_permission"}); //表名，多个表名传数组
        mpg.setStrategy(stragegy);

        mpg.setTemplateEngine(new VelocityTemplateEngine());
        mpg.execute();
    }
}
